package cn.cj.util;

import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Component;

import cn.cj.vo.JobBean;

@Component
public class JsoupUtil {
    /**
     * 获取页面对像
     *
     * @param str
     * @return
     * @throws IOException
     */
    public Document getDocument(String str) throws IOException {

        // 获取请求连接
        Connection con = Jsoup.connect(str);
        // 请求头设置，特别是cookie设置
        con.header("Accept", "text/html, application/xhtml+xml, */*");
        con.header("Content-Type", "application/x-www-form-urlencoded");
        con.header(
                "User-Agent",
                "Chrome：Mozilla/5.0 (Windows NT 5.1; zh-CN) AppleWebKit/535.12 (KHTML, like Gecko) Chrome/22.0.1229.79 Safari/535.12");
        con.header("Cookie", "");
        return con.get();
    }

    /**
     * 获取总页数
     *
     * @param document
     * @return
     */
    public int getPag(Document document) {
        String allpag = document.select(".td").text();
        int a = allpag.indexOf("页");
        int pag = Integer.valueOf(allpag.substring(1, a)).intValue();
        return pag;
    }

    /**
     * 获取页面页码链接
     *
     * @param document
     * @return
     */
    public String getPagURL(Document document) {
        return document.select(".p_in > ul > li > a").first().attr("href");
    }

    /**
     * 获取页面数据集合
     *
     * @param document
     * @return
     * @throws ParseException
     */
    public List<JobBean> cycle(Document document, String string)
            throws ParseException {
        JobBean jobBean = null;
        Elements el = document.select(".dw_table");
        Elements el2 = el.select(".el");
        List<JobBean> jobBeanList = new ArrayList<JobBean>();
        for (int i = 1; i < el2.size(); i++) {
            jobBean = new JobBean();
            jobBean.setSearch(string);
            Elements a = el2.get(i).select(".t1 > span > a");
            String position = a.text();
            jobBean.setPosition(position);
            String positionURL = a.attr("href");
            jobBean.setPositionURL(positionURL);
            Elements a2 = el2.get(i).select(".t2 > a");
            String company = a2.text();
            jobBean.setCompany(company);
            String companyURL = a2.attr("href");
            jobBean.setCompanyURL(companyURL);
            Elements a3 = el2.get(i).select(".t3");
            String address = a3.text();
            jobBean.setAddress(address);
            Elements a4 = el2.get(i).select(".t4");
            String salary = a4.text();
            jobBean.setSalary(salary);
            Elements a5 = el2.get(i).select(".t5 ");
            String jobdate2 = a5.text();
            // DateFormat df = new SimpleDateFormat("MM-dd");
            // Date jobdate = df.parse(jobdate2);
            Calendar aa = Calendar.getInstance();
            jobBean.setJobdate(aa.get(Calendar.YEAR) + "-" + jobdate2);
            // String date2 = df.format(new Date());//格式化数据
//			System.out.println(jobBean);
            jobBeanList.add(jobBean);
        }
        return jobBeanList;
    }

    /**
     * 循环生成页面url
     *
     * @param string
     * @param pag
     * @return
     */
    public List<String> ModifyURL(String string, int pag) {
        List<String> list = new ArrayList<String>();
        int a = string.indexOf("?");
        String url1 = string.substring(0, a);
        for (int i = 1; i <= pag; i++) {
            String html = i + ".html";
            String url = url1.replace("2.html", html);
            list.add(url);
        }
        return list;
    }


}
